;(function(global){
	var constents = {
        chromeIntent: "intent://#Intent;scheme=xishzmq;package=com.xisue.zhoumo;end",
        ios: "wb3903716518://zmqne",
        webIos: "itms-apps://itunes.apple.com/app/id820846113",
        webAndroid: "http://wanzhoumo.com/download?type=2&apk=A2",
        android: "xishzmq://",
        wechat: "http://a.app.qq.com/o/simple.jsp?pkgname=com.xisue.zhoumo",
        web: "http://wanzhoumo.com/",
        h5Activity: location.protocol + "//" + location.host + "/shanghai/huodong/",
        h5search: location.protocol + "//" + location.host + "/m/search/index?type=a&china=1&pinyin=lasa&keyword="
    };

    var loadIframe = null;
    function createIframe() {
        var iframe = document.createElement("iframe");
        iframe.style.cssText = "display:none;width:0px;height:0px;";
        document.body.appendChild(iframe);
        loadIframe = iframe;
    }


    var openApp = function(options) {
        var opts = options || {};
        var ua = navigator.userAgent;

        isIPhone = ua.match(/iPhone|iPod/i) != null;
        isIPad = ua.match(/iPad/i) != null;
        isAndroid = ua.match(/Android/i) != null;
        isChrome = ua.match(/Chrome\/([\d.]+)/) || ua.match(/CriOS\/([\d.]+)/);
        wechat = ua.toLowerCase().match(/micromessenger/);
        qq = ua.toLowerCase().match(/qq/);
        weibo = ua.toLowerCase().match(/weibo/);
        xisue = ua.toLowerCase().match(/com\.xisue\.zhoumo/);
        if(wechat || qq){
            if(opts.type == 'activity'){
                location.href = constents.h5Activity + opts.match;
            }else if(opts.type == 'tag'){
                location.href = constents.h5search + opts.match;
            }
            else{
                location.href = constents.wechat;
            }
        }
        else if (isIPhone || isIPad) {
            if(opts.type == 'activity' || opts.type == 'canjia'){
                loadIframe.src=constents.android + "act?id=" + opts.match;
            }else if(opts.type == 'tag'){
                loadIframe.src=constents.android + "actlist/?sort=distance&tag=" + opts.match;
            }
            else{
                loadIframe.src=constents.ios;
            }
            var loadTime = Date.now();
            setTimeout(function() {
                if (Date.now() - loadTime < 400) {
                    if(opts.type == 'activity'){
                        location.href = constents.h5Activity + opts.match;
                    }else if(opts.type == 'tag'){
                        location.href = constents.h5search + opts.match;
                    }
                    else{
                        location.href = constents.webIos;
                    }
                }
            }, 300);
        }
        else if (isAndroid) {
            if (isChrome) {
                if (opts.type == 'activity' || opts.type == 'canjia') {
                    location.href = "intent://act?id=" + opts.match + "#Intent;scheme=xishzmq;package=com.xisue.zhoumo;end";
                }else if (opts.type == 'tag'){
                    location.href = "intent://actlist/?sort=distance&tag=" + opts.match + "#Intent;scheme=xishzmq;package=com.xisue.zhoumo;end";
                }
                else {
                    location.href = "intent://#Intent;scheme=xishzmq;package=com.xisue.zhoumo;end";
                }
            } else {
                if(opts.type == 'activity' || opts.type == 'canjia'){
                    loadIframe.src = constents.android + "act?id=" + opts.match;
                }else if(opts.type == 'tag'){
                    loadIframe.src=constents.android + "actlist/?sort=distance&tag=" + opts.match;
                }
                else{
                    loadIframe.src = constents.android;
                }
            }
            var loadTime = Date.now();
            setTimeout(function() {
                if (Date.now() - loadTime < 400) {
                    if(opts.type == 'activity'){
                        location.href = constents.h5Activity + opts.match;
                    }else if(opts.type == 'tag'){
                        location.href = constents.h5search + opts.match;
                    }
                    else{
                        location.href = constents.webAndroid;
                    }
                }
            }, 300);
        }
        else {
            if(opts.type == 'activity'){
                location.href = constents.h5Activity + opts.match;
            }else if(opts.type == 'tag'){
                location.href = constents.h5search + opts.match;
            }
            else{
                location.href = constents.web;
            }
        }
    }
    var open = function(o){
        createIframe();
        openApp(o);
    }
    global.openApp = open;
})(typeof window !== 'undefined' ? window : this);
